package com.hylanda.processors.HNGetH2x.util;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;


public class H2xUtil {

	/**
	 * no use
	 * @param url
	 * @return
	 */
	public static JSONObject getContent(String url)
	{
		
		/*replaceCharMap.put("=", "＝");
		replaceCharMap.put("※", "&");
		replaceCharMap.put("&", "%A1%F9");
		replaceCharMap.put("=", "%A3%BD");
		replaceCharMap.put("\r\n", "");*/
		
		//url = url.replaceAll("=", "＝");
		url = url.replaceAll("※", "&");
		url = url.replaceAll("&", "%A1%F9");
		url = url.replaceAll("=", "%A3%BD");
		url = url.replaceAll("\r\n", "");
		
		String p_url = "http://batch888.hylanda.com/h2x/H2xSvr.htm?Cmd=3&0=charset=gbk;img=yes;AbsLen=100;kwdcnt=10;debug=0;pictext=yes;class=yes;outhtm=1;link=0;inc=yes;from=1;kwquot=no;kwtitleof=no;docurrent=no;webimg=no;ampm=no;delblankline=yes;com=4ABAAACG65TEE;user=yangxiaohua1@hylanda.com;&1=";
		String r_url = p_url+url;
		JSONObject jsonObject = new JSONObject();
		try (DefaultHttpClient dclient = new DefaultHttpClient()){
			// 发送get请求
			HttpGet request = new HttpGet(r_url);
			HttpResponse response = dclient.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				String strResult = EntityUtils.toString(response.getEntity(), "gbk");
				jsonObject = XMLUtil.getJson(strResult);
			} else {
				//logger.error("get请求提交失败:" + url);
			}
		} catch (IOException e) {
			//logger.error("get请求提交失败:" + url, e);
		}
		
		return jsonObject;
	}
	
	public static String getContentString(String url)
	{
		//url = url.replaceAll("=", "＝");
		url = url.replaceAll("※", "&");
		url = url.replaceAll("&", "%A1%F9");
		url = url.replaceAll("=", "%A3%BD");
		url = url.replaceAll("\r\n", "");
		String p_url = "http://batch888.hylanda.com/h2x/H2xSvr.htm?Cmd=3&0=charset=gbk;img=yes;AbsLen=100;kwdcnt=10;debug=0;pictext=yes;class=yes;outhtm=1;link=0;inc=yes;from=1;kwquot=no;kwtitleof=no;docurrent=no;webimg=no;ampm=no;delblankline=yes;com=4ABAAACG65TEE;user=yangxiaohua1@hylanda.com;&1=";
		String r_url = p_url+url;
		String strResult = null;
		//JSONObject jsonObject = new JSONObject();
		
		try (
			DefaultHttpClient dclient = new DefaultHttpClient()){
			
			dclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,10000);//连接时间

			dclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,10000);//数据传输时间
			// 发送get请求
			HttpGet request = new HttpGet(r_url);
			HttpResponse response = dclient.execute(request);
			/** 请求发送成功，并得到响应 **/
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				/** 读取服务器返回过来的json字符串数据 **/
				strResult = EntityUtils.toString(response.getEntity(), "gbk");
				//jsonObject = XMLUtil.getJson(strResult);
			} else {
				//logger.error("get请求提交失败:" + url);
			}
		} catch (IOException e) {
			//logger.error("get请求提交失败:" + url, e);
		}finally {
			
		}
		
		return strResult;
	}
	
	public static void main(String[] args) {
		//String url = "http://mp.weixin.qq.com/s?__biz=MTgwNTE3Mjg2MA==&mid=2652532968&idx=3&sn=58313576dcd9fb977d6e5f2b0106a8c9&chksm=5849e7fb6f3e6eedde4bcff70236166127560a19ee87ec9be567973dbff0685de705b2d470c5&scene=27";
		String url = "https://kuaibao.qq.com/s/20180520A0T3A100";
		url = URLHelper.regular(url);
		String string = getContentString(url);
		JSONObject jsonObject = XMLUtil.getJson(string);
		System.out.println(jsonObject.toJSONString());
	}
}
